package com.weishaoying.rws.service;

import com.weishaoying.rws.entity.Book;
import com.weishaoying.rws.dao_r.BookMapper;
import com.weishaoying.rws.dao_r.UserReadMapper;
import com.weishaoying.rws.entity.Dog;
import com.weishaoying.rws.dao_rw.DogMapper;
import com.weishaoying.rws.dao_rw.UserWriteMapper;
import com.weishaoying.rws.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author WeiShaoying
 * @date 2018/5/5
 */
@Service
public class UserService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private UserReadMapper userReadMapper;

    //

    @Autowired
    private DogMapper dogMapper;

    @Autowired
    private UserWriteMapper userWriteMapper;

    //读操作：数据源1

    public List<Book> queryBooks() {
        return bookMapper.queryAll();
    }

    public User queryUser(Long id) {
        System.out.println("----queryUser = [" + id + "]");
        return userReadMapper.queryById(id);
    }

    public List<User> queryAllUser() {
        System.out.println("----queryAll");
        return userReadMapper.queryAll();
    }


    //写操作：数据源2-这里扩展了，不再单纯区分读写分离，而是测试 不同数据源如何绑定不同的Mapper

    public List<Dog> queryDogs() {
        return dogMapper.queryAll();
    }

    public void saveUser(User user) {
        System.out.println("----SaveUser = [" + user + "]");
        userWriteMapper.saveUser(user);
    }

    public void deleteUser(Long id) {
        System.out.println("----deleteUser = [" + id + "]");
        userWriteMapper.deleteById(id);
    }

    public void updateUser(User user) {
        System.out.println("----updateUser = [" + user + "]");
        userWriteMapper.updateUser(user);
    }
}
